package header

import "edit"
import "panel"
import "⚛sdl"

type HeaderPanel struct {
    edit.EditPanel
    Do panel.Doer
    Other panel.Type
}

func NewHeaderPanel(d panel.Doer, other panel.Type) *HeaderPanel {
    return &HeaderPanel{Do: d, Other: other}
}

func (h *HeaderPanel) Show(message string) {
	h.EditPanel.Content[0] = message
	h.Refresh()
}

func (h *HeaderPanel) FocusForClick(line, col int) panel.Type {
    h.Cursor(line, col)
    return h
}

func (h *HeaderPanel) Resize(lines, cols int, loff, coff int) {
    h.EditPanel.Resize(lines, cols - 3, 0, coff + 3)  
}

func (h *HeaderPanel) Refresh() {
	panel.Log("HeaderPanel.Refresh")
	content := h.EditPanel.Content
	if len(content) == 0 { content = []string{""} }
    // RESTORE h.Put(0, -3, curses.ACS_HLINE)
    // RESTORE h.Put(0, -2, curses.ACS_RTEE)
    h.EditPanel.Refresh()
    // RESTORE for col := 2 + len(content[0]); col < h.Cols; col += 1 { h.Put(0, col, curses.ACS_HLINE) }
}

func (h *HeaderPanel) Key(rune int) panel.Type {
    switch rune {
    case sdl.K_RETURN:
        command := h.EditPanel.Content[0]
        h.EditPanel.Content[0] = ""
        h.EditPanel.Line, h.EditPanel.Col = 0, 0
        h.Refresh()
        h.Cursor(0, 0)
        if h.Do != nil { h.Do.Do(command) }
        return h.Other
        
    case 'g' - 'a' + 1:
    	return h.Other
        
    default:
        h.EditPanel.Key(rune)
    }
    return nil
}

